package com.dg11185.nearshop.baidumap.nearshop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.nearshop.R;
import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BaiduMap.OnMapStatusChangeListener;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.model.LatLng;
import com.dg11185.nearshop.baidumap.BaseMapActivity;
import com.dg11185.nearshop.baidumap.MapConstant;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.view.ViewHelper;

/**
 * 
 * @author：Daibo
 * @time：2014-9-26 上午11:45:09
 */
public class NearshopView extends BaseMapActivity implements INearshopView, OnClickListener {

    /** 选择栏 */
    private ListView listView;

    private TextView txtIndustry;

    public int categoryIndex = 0;
    
    private INearshopPresenter presenter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new NearshopPresenter(this);
        listView = (ListView) this.findViewById(R.id.nearshop_list);
        txtIndustry = (TextView) this.findViewById(R.id.nearshop_industry);
        findViewById(R.id.nearshop_txt_layout).setOnClickListener(this);
        findViewById(R.id.nearshop_back).setOnClickListener(this);
        findViewById(R.id.nearshop_data_detail).setOnClickListener(this);
        findViewById(R.id.nearshop_data_route).setOnClickListener(this);

        addLocOverlay();
        initListView();
        presenter.initCountOverlay();
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.map_nearshop);
    }

    /**
     * 
     */
    @Override
    public void onReceiveLocation(BDLocation location) {
        // TODO Auto-generated method stub
        super.onReceiveLocation(location);
    }

    /**
     * 筛选商家类型的listview点击事件回调
     */
    private OnItemClickListener itemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            presenter.onCategoryItemClick();
        }
    };

    //
    // /**
    // * 摇一摇时间回调
    // */
    // private ShakeListener shakeListener = new
    // ShakeListener(NearshopView.this){
    // public void onShake() {
    // presenter.onShake();
    // };
    // };
    //

    /**
     * 百度地图图层点击事件，包括商家图层和商家数量图层
     */
    private OnMarkerClickListener markerClickListener = new OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker arg0) {
            presenter.onMarkerClick(arg0);
            return false;
        }
    };

    /**
     * 百度地图点击事件，点击后完成
     */
    private OnMapClickListener mapClickListener = new OnMapClickListener() {

        @Override
        public boolean onMapPoiClick(MapPoi arg0) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onMapClick(LatLng arg0) {
            // TODO Auto-generated method stub

        }
    };

    /**
     * 地图被放大缩小或者被拖动时会回调该方法
     */
    private OnMapStatusChangeListener mapStatusChangeListener = new OnMapStatusChangeListener() {

        @Override
        public void onMapStatusChangeStart(MapStatus arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onMapStatusChangeFinish(MapStatus arg0) {
            presenter.onMapStatusChanged(arg0);
        }

        @Override
        public void onMapStatusChange(MapStatus arg0) {
            // TODO Auto-generated method stub

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.nearshop_txt_layout:
            ViewHelper.setPivotX(listView, 0);
            ViewHelper.setPivotY(listView, 0);
            if (View.GONE == listView.getVisibility()) {
                ObjectAnimator.ofFloat(listView, "scaleY", 0, 1).setDuration(200).start();
                txtIndustry.setCompoundDrawablesWithIntrinsicBounds(null, null,
                        getResources().getDrawable(R.drawable.ic_white_arrow_small_up), null);
                listView.setVisibility(View.VISIBLE);
            } else {
                txtIndustry.setCompoundDrawablesWithIntrinsicBounds(null, null,
                        getResources().getDrawable(R.drawable.ic_white_arrow_small_down), null);
                ObjectAnimator oa = ObjectAnimator.ofFloat(listView, "scaleY", 1, 0);
                oa.addListener(new ListAnimListener());
                oa.setDuration(200).start();
            }
            break;
        case R.id.nearshop_back:
            this.finish();
            break;
        case R.id.nearshop_data_detail:
            break;
        case R.id.nearshop_data_route:
            break;
        case R.id.nearshop_refresh:
        default:
            break;
        }
    }

    @Override
    public void displayShopOverlay() {
        // TODO Auto-generated method stub

    }

    @Override
    public void displayCategoryList() {
        // TODO Auto-generated method stub

    }

    /**
     * 隐藏商家类型的方法，当商家类型的listview展开时点击List或者地图隐藏
     */
    private void hideCategoryList() {

    }

    @Override
    public void displayCountOverlay() {
        // TODO Auto-generated method stub

    }

    @Override
    public void displayShopDetailView() {
        // TODO Auto-generated method stub

    }

    @Override
    public void showLoadingDialog() {
        // TODO Auto-generated method stub

    }

    @Override
    public void hideLoadingDialog() {
        // TODO Auto-generated method stub

    }

    private void initListView() {
        ArrayList<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
        for (int a = 0; a < MapConstant.industrys.length; a++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("CONTENT", MapConstant.industrys[a]);
            listData.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(this, listData, R.layout.map_nearshop_item,
                new String[] { "CONTENT" }, new int[] { R.id.nearshop_shop_item_txt });
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String industryName = MapConstant.industrys[position];
                String industryId = MapConstant.getIndustryMap().get(industryName);

                txtIndustry.setCompoundDrawablesWithIntrinsicBounds(null, null,
                        getResources().getDrawable(R.drawable.ic_white_arrow_small_down), null);
                ObjectAnimator oa = ObjectAnimator.ofFloat(listView, "scaleY", 1, 0);
                oa.addListener(new ListAnimListener());
                oa.setDuration(200).start();
            }
        });
    }

    private class ListAnimListener extends AnimatorListenerAdapter {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            listView.setVisibility(View.GONE);
        }
    }

}
